/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.Impl;

import com.startup.musicstore.model.CreditCard;
import com.startup.musicstore.model.Customer;
import com.startup.musicstore.model.CustomerInvoice;
import com.startup.musicstore.model.Orders;
import com.startup.musicstore.services.CreditCardApprovalService;
import com.startup.musicstore.services.SuccessfulPurchaseService;
import com.startup.musicstore.services.crud.CreditCardCrudService;
import com.startup.musicstore.services.crud.CustomerCrudService;
import com.startup.musicstore.services.crud.CustomerInvoiceCrudService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author linda
 */
@Service("successfulPurchaseService")
public class SuccessfulPurchaseServiceImpl implements SuccessfulPurchaseService{
    
     @Autowired
    private CustomerCrudService customerCrudService;

    @Autowired
    private CreditCardCrudService creditCardCrudService;
    
    @Autowired
    private CustomerInvoiceCrudService customerInvoiceCrudService;
    
    @Autowired
    private CreditCardApprovalService creditCardApprovalService;

    @Override
    public void PayInvoice() {
       List<Customer> customers = customerCrudService.findAll();
       
        for (Customer customer : customers) 
        {
            //first for each customer get the list of cards
            List<CreditCard> cards = customer.getCards();
            //create a list to store all approved cards
            List<CreditCard> appCreditCards = new ArrayList<CreditCard>();
            
            for (CreditCard creditCard : cards) 
            {
                //run through the cards and run the process payment service given
                //this will check which creditcards are approved
                String status = creditCardApprovalService.processPayment(creditCard.getCreditNumber(), creditCard.getBalance(), creditCard.getExpiryDate());
                if (status.equalsIgnoreCase("APPROVED")) 
                {
                    //store all approved cards here
                    appCreditCards.add(creditCard);
                }
            }
            
            //if the approved cards list is not empty
            if (!appCreditCards.isEmpty()) {
                //get the list of orders for this particular customer
                List<Orders> orderList = customer.getOrders();
                for (Orders order : orderList) {
                    //create object for merge
                    CustomerInvoice customerInvoice = order.getCustomerInvoice();
                    //Use the very first approved credit card
                    //also create a object for the merge purposes
                    CreditCard creditCard = appCreditCards.get(0); //Very first approved CreditCard
                    
                    //do the calculations to get the new balances
                    BigDecimal total = order.getCustomerInvoice().getOrderAmount();
                    BigDecimal balance = creditCard.getBalance();
                    
                    double difference = balance.doubleValue() - total.doubleValue();
                    
                    //merge the new data
                    creditCard.setBalance(BigDecimal.valueOf(difference));
                    creditCardCrudService.merge(creditCard);
                    
                    customerInvoice.setInvoiceStatus("PAID");
                    customerInvoiceCrudService.merge(customerInvoice);
                }
            } else {
                
            }
       
        }
  
        
    }
    
}
